package cn.qingxuntech.console.service

import cn.qingxuntech.console.entity.SspMedia
import cn.qingxuntech.console.mapper.SspMediaMapper
import cn.qingxuntech.console.model.request.SspMediaRequest
import com.mybatisflex.core.paginate.Page
import com.mybatisflex.core.query.QueryWrapper
import com.mybatisflex.kotlin.extensions.db.update
import com.mybatisflex.kotlin.extensions.kproperty.eq
import com.mybatisflex.kotlin.extensions.wrapper.whereWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import javax.management.Query

@Service
class SspMediaService {

    @Autowired
    lateinit var sspMediaMapper: SspMediaMapper

    /**
     * 新增
     */
    fun add(sspMedia: SspMedia) {
        sspMediaMapper.insert(sspMedia)
    }

    /**
     * 分页
     */
    fun page(sspMediaRequest: SspMediaRequest): Page<SspMedia> {

        return sspMediaMapper.paginate(Page.of(sspMediaRequest.current, sspMediaRequest.size),
            QueryWrapper.create()
                .like(SspMedia::name, sspMediaRequest.name)
                .orderBy(SspMedia::createTime, false))
    }

    fun list(): List<SspMedia> {

        return sspMediaMapper.selectListByQuery(QueryWrapper.create().orderBy(SspMedia::createTime, false))
    }

    fun updateMediaCostRate(sspMedia: SspMedia) {

        update<SspMedia> {
            SspMedia::mediaCostRate set sspMedia.mediaCostRate
            whereWith {
                SspMedia::id eq sspMedia.id
            }
        }

    }
}